const jsonData = require("../jsondata.js");
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({
 
	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		addOrMod: 'add',
		//全身性疾病选项集
		HypertensionIndex: -1,
		isDiseaseHappenIndex: -1,
		heartDiseaseStatusIndex: -1,
		diabetesStatusIndex: -1,
		diabetesFirstStatusIndex: -1,
		notOrHave:jsonData.notOrHave,
		stableOrFluctuating:jsonData.stableOrFluctuating,
		highOrNormal:jsonData.highOrNormal,
    schoolId:'',
		/*** 全身性疾病史* */
		//全身性疾病有无发生
		isDiseaseHappen: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0009',
			new_contentid: 'NR0018',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//半年内发病病名
		attackName: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0009',
			new_contentid: 'NR0010',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//近期服用药物
		recentDrugUse: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0009',
			new_contentid: 'NR0020',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//高血压发病年数
		hypertensionYear: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0007',
			new_contentid: 'NR0015',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//近期血压情况
		hypertensionStatus: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0007',
			new_contentid: 'NR0013',
			new_result: 'SC0000000061',
			new_doctorid: null,
			ownerid: null
		},
		//心脏病得病年数
		heartDiseaseYear: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0013',
			new_contentid: 'NR0016',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//半年内有无发病
		heartDiseaseStatus: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0013',
			new_contentid: 'NR0008',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//糖尿病得病年数
		diabetesYear: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0012',
			new_contentid: 'NR0017',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//血糖近期情况
		diabetesStatus: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0012',
			new_contentid: 'NR0012',
			new_result: 'SC0000000061',
			new_doctorid: null,
			ownerid: null
		},
		//初次发生血糖情况
		diabetesFirstStatus: {
			new_scid: null,
			new_categoryid: 'LB0006',
			new_itemid: 'XM0012',
			new_contentid: 'NR0028',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getSystemicdiseasesResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
	},
	//提交全身性疾病
	systemicDiseasesSubmit: function() {
		var createResult = this.getSystemicDiseases();
		var that = this;
		commonutil.createScreenResults(createResult, function(res) {
			var idArray = res.result.split('|');
			for(var i = 0;i < idArray.length;i++){
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				isDiseaseHappen:	createResult[0],
				attackName:	createResult[1],
				recentDrugUse:	createResult[2],
				hypertensionYear:	createResult[3],
				hypertensionStatus:	createResult[4],
				heartDiseaseYear:	createResult[5],
				heartDiseaseStatus:	createResult[6],
				diabetesYear:	createResult[7],
				diabetesStatus:	createResult[8],
				diabetesFirstStatus:	createResult[9],
			});

		});
	},
	//更新
	systemicDiseasesUpdate:function(){
		var updateResult = this.getSystemicDiseases();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getSystemicdiseasesResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title: '加载中...',
			mask: true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if (reusltArray.length == 0) {
			wx.hideLoading();
			return;
		}
		this.setData({
			addOrMod: 'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0018') {
				this.setData({
					isDiseaseHappen: element
				})
			} else if (element.new_contentid === 'NR0010') {
				this.setData({
					attackName: element
				})
			} else if (element.new_contentid === 'NR0020') {
				this.setData({
					recentDrugUse: element
				})
			} else if (element.new_contentid === 'NR0015') {
				this.setData({
					hypertensionYear: element
				})
			} else if (element.new_contentid === 'NR0013') {
				this.setData({
					hypertensionStatus: element
				})
			} else if (element.new_contentid === 'NR0016') {
				this.setData({
					heartDiseaseYear: element
				})
			} else if (element.new_contentid === 'NR0008') {
				this.setData({
					heartDiseaseStatus: element
				})
			} else if (element.new_contentid === 'NR0017') {
				this.setData({
					diabetesYear: element
				})
			} else if (element.new_contentid === 'NR0012') {
				this.setData({
					diabetesStatus: element
				})
			} else if (element.new_contentid === 'NR0028') {
				this.setData({
					diabetesFirstStatus: element
				})
			}
		}
		wx.hideLoading();
	},

	/**全身性疾病文本/选项集赋值 */
	onHypertensionYearChange: function(e) {
		var result = 'hypertensionYear.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onAttackNameChange: function(e) {
		var result = 'attackName.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onRecentDrugUseChange: function(e) {
		var result = 'recentDrugUse.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onHeartDiseaseYearChange: function(e) {
		var result = 'heartDiseaseYear.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onDiabetesYearChange: function(e) {
		this.setData({
			['diabetesYear.new_result']: e.detail,
		})
	},
	onIsDiseaseHappenChange: function(e) {
		var result = 'isDiseaseHappen.new_result';
		this.setData({
			[result]:'',
			isDiseaseHappenIndex: e.detail.index
		})
	},
	onHypertensionStatusChange: function(e) {
		this.setData({
			HypertensionIndex: e.detail.index
		})
	},
	onHeartDiseaseStatusChange: function(e) {
		this.setData({
			heartDiseaseStatusIndex: e.detail.index
		})
	},
	onDiabetesStatusChange: function(e) {
		this.setData({
			diabetesStatusIndex: e.detail.index
		})
	},
	onDiabetesFirstStatusChange: function(e) {
		this.setData({
			diabetesFirstStatusIndex: e.detail.index
		})
	},
	//全身性疾病
	getSystemicDiseases: function() {
		var createResult = [];
		var isDiseaseHappen = this.data.isDiseaseHappen;
		var hypertensionStatus = this.data.hypertensionStatus;
		var heartDiseaseStatus = this.data.heartDiseaseStatus;
		var diabetesStatus = this.data.diabetesStatus;
		var diabetesFirstStatus = this.data.diabetesFirstStatus;
		var hypertensionYear = this.data.hypertensionYear;
		var heartDiseaseYear = this.data.heartDiseaseYear;
		var diabetesYear = this.data.diabetesYear;
		var attackName = this.data.attackName;
		var recentDrugUse = this.data.recentDrugUse;

		var scid = this.data.scid;
		var customerid = this.data.customerid;
		//半年内是否发过相关疾病
		var isDiseaseHappenIndex = this.data.isDiseaseHappenIndex;
		var notOrHave = this.data.notOrHave;
		if (isDiseaseHappenIndex >-1) {
			isDiseaseHappen.new_result = notOrHave[isDiseaseHappenIndex];
		} 
		//高血压
		var HypertensionIndex = this.data.HypertensionIndex;
		var stableOrFluctuating = this.data.stableOrFluctuating;
		if (HypertensionIndex>-1) {
			hypertensionStatus.new_result = stableOrFluctuating[HypertensionIndex];
		} 
		//心脏病
		var heartDiseaseStatusIndex = this.data.heartDiseaseStatusIndex ;
		if (heartDiseaseStatusIndex >-1) {
			heartDiseaseStatus.new_result = notOrHave[heartDiseaseStatusIndex];
		}
		//糖尿病
		var diabetesStatusIndex = this.data.diabetesStatusIndex;
		if (diabetesStatusIndex >-1) {
			diabetesStatus.new_result = stableOrFluctuating[diabetesStatusIndex];
		} 
		//第一次糖尿病的血糖状态
		var diabetesFirstStatusIndex = this.data.diabetesFirstStatusIndex;
		var highOrNormal = this.data.highOrNormal;
		if (diabetesFirstStatusIndex>-1) {
			diabetesFirstStatus.new_result = highOrNormal[diabetesFirstStatusIndex];
		}
		
		createResult.push(isDiseaseHappen);
		if(isDiseaseHappen.new_result=='SC0000000008'){
			if(attackName.new_result!=''){
				createResult.push(attackName);
			}
			if(recentDrugUse.new_result!=''){
				createResult.push(recentDrugUse);
			}
		}
		if(hypertensionYear.new_result!=''){
			createResult.push(hypertensionYear);
		}	
		createResult.push(hypertensionStatus);
		if(heartDiseaseYear.new_result!=''){
			createResult.push(heartDiseaseYear);
		}	
		createResult.push(heartDiseaseStatus);
		if(diabetesYear.new_result!=''){
			createResult.push(diabetesYear);
		}	
		createResult.push(diabetesStatus);
		createResult.push(diabetesFirstStatus);
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	},

})
